package com.yunhe.option.thread;

import com.yunhe.common.util.RedisClient;
import com.yunhe.option.domain.base.Enum;
import com.yunhe.option.service.base.EnumService;
import com.yunhe.option.service.base.ItemService;
import com.yunhe.option.utils.SpringContextUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @ClassName DoRelationCache
 * @Description TODO
 * @Author HeTao
 * @Date 2021/3/17 9:37
 * @Version 1.0
 **/
@Component
@DependsOn(value = {"springContextUtil","enumServiceImpl","redisClient"})
public class DoRelationCache  {

    private static Logger logger = LoggerFactory.getLogger(DoRelationCache.class);
    private static final int threadNum = 10;
    private static ExecutorService executor = Executors.newFixedThreadPool(threadNum);


    @PostConstruct
    public void doCache(){
        ExecutorService executor = Executors.newFixedThreadPool(10);
        EnumService enumService = SpringContextUtil.getBean("enumServiceImpl");
        RedisClient redisUtil = SpringContextUtil.getBean("redisClient");
        //用于设置表示位，方便程序启动完毕其他程序进行判断
        String isFinished = "false";
        redisUtil.set("isFinished",isFinished);
        //获取某表的数据总量
        int countNum = enumService.getTotalCount();
        List<Map<Integer,String>> lists = getThreadNum(countNum);
        int j = 0;
        try{
            logger.info("缓存写入数据开始 .......");
            for (int i = 0; i < lists.size(); i++) {
                j ++;
                Map<Integer,String> single =  lists.get(i);
                executor.execute(new DoCacheJob(single,j));
            }
            logger.info("缓存写入数据结束 .......");
            redisUtil.del("isFinished");
            redisUtil.set("isFinished","true");
        }catch (Exception e){
            e.printStackTrace();
            logger.info("线程执行缓存写入数据出错，请及时处理 .......");
        }
        executor.shutdown();
    }

    //根据数据分量为不同执行线程分配不同数量的处理数据
    public static List<Map<Integer,String>> getThreadNum(int count){
        List<Map<Integer,String>> resMap = new ArrayList<>();
        Map<Integer,String> map = new HashMap<>();
        int numOffset = (count/threadNum)+1;
        //每一份数据的数量是numOffset
        for(int i=1;i<=threadNum;i++){
            if(i==1){
                map.put(i,i + ":" + numOffset * i);
                resMap.add(map);
            }
            else if(i>1 && i<= threadNum){
                int index = numOffset * (i - 1) + 1;
                if(count >= index){
                    map.put(i,(numOffset * (i-1) + 1) + ":" + numOffset * i);
                    resMap.add(map);
                }
            }else{
                map.put(i,(numOffset * (i-1) + 1) + ":" + count);
                resMap.add(map);
            }
        }
        return resMap;
    }

//    @Override
//    public void afterPropertiesSet() throws Exception {
//        doCache();
//    }
}
